home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Files / resolveRelativeAlias / resolveRelativeAlias.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  4.4 KB  |  126 lines  |  [TEXT/MPS ]

  1. { resolveRelativeAlias MPW Tool }
  2.  
  3. {   Scenario:
  4.  
  5.   A document contains an alias to a file in a known folder.  The problem
  6.   is that the document might be on a floppy, and the known folder might
  7.   be on a drive whose name is unknown.
  8.   
  9.   For example, say the boot drive will contain "Super Folder" in the root
  10.   directory, and "Super File" in that folder.  The goal is to create an
  11.   alias to "Super File" which will work regardless of the drive's name.
  12.   
  13.     Solution:
  14.   
  15.   Create an alias to the the target (e.g. "SuperFile") which is relative to
  16.   the System file.  This presumes that the folder structure of the drive
  17.   is known, but makes no presumption about the name of the drive.
  18.   
  19.   This sample tool demonstrates the steps involved in creating and resolving
  20.   the alias.  Instead of creating a document to store the alias, the tool wimps
  21.   out and stores the alias in a preference file.
  22.   
  23.   The tool puts up a StandardGetFile dialog.  Selecting a file creates the
  24.   preference file containing the relative alias; clicking Cancel instead
  25.   gets the preference file, resolves it, and prints the vRefNum and name
  26.   of the target.
  27.   
  28.     Alternative strategies:
  29.   
  30.   Well, you could call PBGetVInfo to find the name of the boot volume
  31.   and then build a full pathname, or SetVol to the root directory of the
  32.   boot volume and then use a relative pathname.  But both of those
  33.   solutions are ugly and un-Seven like.
  34.  
  35. }
  36.   
  37. { MPW Tool.  Requires System 7. }
  38.  
  39. PROGRAM resolveRelativeAlias;
  40.  
  41. USES Processes, Files, Aliases, Packages, Script, Resources, Errors, StandardFile, Folders;
  42.  
  43.     VAR
  44.         retCode: OSErr;
  45.         mySFR: StandardFileReply;
  46.         mySFTypeList: SFTypeList;
  47.         sysVRefNum, prefVRefNum, myResRefNum: Integer;
  48.         sysDirID, prefDirID: LongInt;
  49.         targetFSSpec, resolvedFSSpec, sysFSSpec, fileFSSpec: FSSpec;
  50.         targetAliasHandle: AliasHandle;
  51.         changeFlag: Boolean;
  52.     
  53.     PROCEDURE Fail(msg: Str255; retCode: OSErr);
  54.     BEGIN
  55.         WriteLn(msg, ' failed: ', retCode);
  56.         EXIT(resolveRelativeAlias);
  57.     END;
  58.  
  59.     PROCEDURE PrintFSSpec(theFSSpec: FSSpec);
  60.     BEGIN
  61.         WriteLn('FSSpec: ', theFSSpec.vRefNum, ' ', theFSSpec.parID, ' ',
  62.             theFSSpec.name);
  63.     END;
  64.  
  65. BEGIN
  66.     InitGraf(@thePort);
  67.     
  68.     { find the vRefNum and dirID of the system and preferences folders }
  69.     retCode := FindFolder(kOnSystemDisk, kSystemFolderType, false,
  70.         sysVRefNum, sysDirID);
  71.     IF retCode <> noErr THEN Fail('FindFolder', retCode);
  72.  
  73.     retCode := FindFolder(kOnSystemDisk, kPreferencesFolderType, true,
  74.         prefVRefNum, prefDirID);
  75.     IF retCode <> noErr THEN Fail('FindFolder2', retCode);
  76.  
  77.     { make a FSSpec for the system folder }
  78.     retCode := FSMakeFSSpec(sysVRefNum, sysDirID, 'System', sysFSSpec);
  79.     IF retCode <> noErr THEN Fail('FSMakeFSSpec', retCode);
  80.         
  81.     { get a file to point the alias at }
  82.     StandardGetFile(nil, -1, mySFTypeList, mySFR);
  83.     
  84.     IF mySFR.sfGood THEN { got a file }
  85.         BEGIN
  86.             { make an alias to the file relative to the system file }
  87.             retCode := NewAlias(@sysFSSpec, mySFR.sfFile, targetAliasHandle);
  88.             IF retCode <> noErr THEN Fail('NewAlias', retCode);
  89.             
  90.             { create the preferences file }
  91.             retCode := FSMakeFSSpec(prefVRefNum, prefDirID, 'My Alis Preference', fileFSSpec);
  92.             FSpCreateResFile(fileFSSpec, '????', 'pref', smRoman);
  93.             retCode := ResError;
  94.             IF retCode <> noErr THEN Fail('FSpCreateResFile', retCode);
  95.             
  96.             { add the alias to the preferences file }
  97.             myResRefNum := FSpOpenResFile(fileFSSpec, fsRdWrPerm);
  98.             IF myResRefNum = -1 THEN Fail('FSpOpenResFile', ResError);
  99.  
  100.             AddResource(Handle(targetAliasHandle), rAliasType, 128,'');
  101.             WriteResource(Handle(targetAliasHandle));
  102.             CloseResFile(myResRefNum);
  103.             WriteLn('done');
  104.         END
  105.     ELSE { user clicked cancel, get the alias from preferences and resolve it }
  106.         BEGIN
  107.             { open the preferences file and get the alias }
  108.             retCode := FSMakeFSSpec(prefVRefNum, prefDirID,
  109.                                     'My Alis Preference', fileFSSpec);
  110.             IF retCode <> noErr THEN Fail('FSMakeFSSpec', retCode);
  111.  
  112.             myResRefNum := FSpOpenResFile(fileFSSpec, fsRdPerm);
  113.             IF myResRefNum = -1 THEN Fail('FSpOpenResFile', ResError);
  114.  
  115.             targetAliasHandle := AliasHandle(GetResource(rAliasType, 128));
  116.             IF targetAliasHandle = NIL THEN Fail('GetResource', ResError);
  117.             
  118.             { resolve the alias relative to the system file }
  119.             retCode := ResolveAlias(@sysFSSpec, targetAliasHandle, resolvedFSSpec, changeFlag);
  120.             IF retCode <> noErr THEN Fail('ResolveAlias', retCode);
  121.             
  122.             Write('resolves to: ');
  123.             PrintFSSpec(resolvedFSSpec);
  124.             CloseResFile(myResRefNum);
  125.         END;
  126. END.